package com.cornergj.back.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cornergj.chat.webstock.MyWebSocketOtO;
import com.cornergj.model.Admin;
import com.cornergj.model.AuditApplication;
import com.cornergj.model.Entity;
import com.cornergj.model.Entity.AdminCriteria;
import com.cornergj.model.Entity.AuditApplicationCriteria;
import com.cornergj.model.Entity.NannyCriteria;
import com.cornergj.model.Entity.OrderServiceCriteria;
import com.cornergj.model.Entity.Pagination;
import com.cornergj.model.Entity.UserBasicInfoCriteria;
import com.cornergj.model.Entity.Value;
import com.cornergj.model.Nanny;
import com.cornergj.model.OrderService;
import com.cornergj.model.SiteOfCity;
import com.cornergj.model.SiteOfProvince;
import com.cornergj.model.UserBasicInfo;
import com.cornergj.model.WorkType;
import com.cornergj.response.ErrorCode;
import com.cornergj.response.JsonResult;
import com.cornergj.service.AdminService;
import com.cornergj.service.AuditApplicationService;
import com.cornergj.service.NannyService;
import com.cornergj.service.OrderServiceService;
import com.cornergj.service.SiteOfCityService;
import com.cornergj.service.SiteOfProvinceService;
import com.cornergj.service.UserBasicInfoService;
import com.cornergj.service.WorkTypeService;
import com.cornergj.uitl.CryptUtil;
import com.cornergj.uitl.FileUtils;


/**
 * Title:
 * Model:
 * Description:
 * @author 郑耿杰
 * @date 2018年1月31日
 */
@Controller
@RequestMapping("/nanny")
public class NannyController {
	
	@Autowired
	private AuditApplicationService auditApplicationService;
	@Autowired
	private WorkTypeService workTypeService;
	@Autowired
	private SiteOfProvinceService siteOfProvinceService;
	@Autowired
	private SiteOfCityService siteOfCityService;
	@Autowired
	private NannyService nannyService;
	@Autowired
	private OrderServiceService orderServiceService;
	/**
	 * 分页方法
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(HttpServletRequest request, Nanny bean, Pagination pagination) {
		//查询条件封装
		NannyCriteria criteria = new NannyCriteria();
		if(bean != null && bean.getTruename()!= null && !"".equals(bean.getTruename())){
			criteria.setTruename(Value.like("%"+bean.getTruename()+"%"));
		}
		//分页查询
		Pagination list = nannyService.selectPage(criteria, pagination);
		request.setAttribute(bean.getTruename()+"pagination", list);
		request.setAttribute("bean", bean);
		return "nanny/list";
	}
	/**
	 * 查看方法
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String view(HttpServletRequest request,int nid, Pagination pagination) {
		Nanny nanny = nannyService.selectOne(nid);
		WorkType workType=workTypeService.selectOne(nanny.getWorkTypeId());
		SiteOfProvince province=siteOfProvinceService.selectOne(nanny.getServiceProvince());
		SiteOfCity city=siteOfCityService.selectOne(nanny.getServiceCity());
		request.setAttribute("province", province);
		request.setAttribute("city", city);
		request.setAttribute("workType", workType);
		request.setAttribute("nanny", nanny);
		return "nanny/view";
	}
	/**
	 * 上传页面
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/touploadimg", method = RequestMethod.GET)
	public String touploadimg(HttpServletRequest request,int nid,Pagination pagination) {
		Nanny nanny = nannyService.selectOne(nid);
		request.setAttribute("bean", nanny);
		return "nanny/uploadimg";
	}
	/*
	 * 上传头像
	 */
	@RequestMapping(value = "/uploadimg", method = RequestMethod.POST)
	public @ResponseBody JsonResult uploadimg(HttpServletRequest request,int nid,@RequestParam(value = "nannyfile", required = false) MultipartFile nannyfile,HttpServletResponse response) {
		JsonResult result = new JsonResult();
		Nanny nanny = nannyService.selectOne(nid);
		try{
			if(nannyfile!=null){
				byte[] multipartbackfilebytes=nannyfile.getBytes();
				String nannyfileimg_key=FileUtils.uploadFileBybyte(multipartbackfilebytes);
				String nannyfileimg_url=FileUtils.getFileUrls(nannyfileimg_key);
				nanny.setNannyKey(nannyfileimg_key);
				nanny.setNannyUrl(nannyfileimg_url);
				nannyService.update(nanny);
			}
		}catch(Exception e){
			e.printStackTrace();
			result.addErrorCode(ErrorCode.UPLOAD_FILE_FAIL_ERROR);
		}
		return result;
	}
	/*
	 * 返回保姆信息
	 */
	@RequestMapping(value = "/getnannyinfo", method = RequestMethod.GET)
	public @ResponseBody JsonResult getnannyinfo(HttpServletRequest request,int nid,HttpServletResponse response) {
		JsonResult result = new JsonResult();
		Nanny nanny = nannyService.selectOne(nid);
		try{
			result.setData(nanny.getNannyUrl());
		}catch(Exception e){
			e.printStackTrace();
			result.addErrorCode(ErrorCode.UPLOAD_FILE_FAIL_ERROR);
		}
		return result;
	}
	/*
	 * 解约保姆
	 */
	@RequestMapping(value = "/surrender", method = RequestMethod.GET)
	public @ResponseBody JsonResult surrender(HttpServletRequest request,int nid,HttpServletResponse response) {
		JsonResult result = new JsonResult();
		try{
			Nanny nanny = nannyService.selectOne(nid);
			OrderServiceCriteria criteria=new OrderServiceCriteria();
			criteria.setArrangeNannyId(Value.eq(nid));
			List<OrderService> orderlist=orderServiceService.selectList(criteria);
			if(orderlist.size()>0){
				result.addErrorCode(ErrorCode.DELNANNY_FAIL_HASSERVICE);
			}else{
				String idcardkey=nanny.getIdentityImgKey();
				String imgkey=nanny.getNannyKey();
				if(imgkey!=null){
					if(FileUtils.deleteFile(idcardkey) && FileUtils.deleteFile(imgkey)){
						nannyService.delete(nid);
					}else{
						result.addErrorCode(ErrorCode.DELNANNY_FAIL_HASSERVICE);
					}
				}else{
					if(FileUtils.deleteFile(idcardkey)){
						nannyService.delete(nid);
					}else{
						result.addErrorCode(ErrorCode.DELNANNY_FAIL_HASSERVICE);
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			result.addErrorCode(ErrorCode.DELNANNY_FAIL_HASSERVICE);
		}
		return result;
	}
}
